package com.besttone.action;

import com.besttone.bean.SecurityFlashSmsVO;
import com.besttone.bean.SecurityFlashSmsVO_old;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.*;
import com.jfinal.core.Controller;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocumentList;

import java.util.List;

/**
 * Created by zhengzhenzhen on 2017/4/20.
 * http://localhost:9090/signalAndsms_2_solr/flashsms/queryFlashSms?called_number=18018606320
 * 查询安全闪信下发记录
 */
public class SecurityFlashSmsAction extends Controller {
    private static Log _log = LogFactory.getLog(SecurityFlashSmsAction.class);
    /**
     * 查询安全闪信列表
     */
    @SuppressWarnings("rawtypes")
    public void queryFlashSms() {
        String streaming_no = getPara("streaming_no");
        String sp_account = getPara("sp_account");
        String sp_pwd = getPara("sp_pwd");//密码：MD5(streaming_no&sp_account&"flashsms")
        String calling_number = getPara("calling_number");// 主叫
        String called_number = getPara("called_number");// 被叫
        String send_number = getPara("send_number");
        String receive_number = getPara("receive_number");
        String flash_send_number = getPara("flash_send_number");
        String create_user = getPara("create_user");
        String product_id=getPara("product_id");

        String signal_type=getPara("signal_type");
        String province_code=getPara("province_code");
        String sms_create_time_from = getPara("send_time_from");// 发送时间开始：按天
        String sms_create_time_to = getPara("send_time_to");// 发送时间结束：按天
        String receive_time_from=getPara("receive_time_from");//接收时间开始：按天
        String receive_time_to=getPara("receive_time_to");// 接收时间结束：按天
        String mt_status = getPara("mt_status");// 下发状态
        String current_page = StringUtils.isBlank(getPara("current_page")) ? "1"
                : getPara("current_page");// 当前页数
        String per_page_size = StringUtils.isBlank(getPara("per_page_size")) ? "10"
                : getPara("per_page_size");// 每页条数，默认10
        StringBuffer pwdBuf=new StringBuffer();
        String nowpwd= MD5Util.md5(pwdBuf.append(streaming_no).append("&").append(sp_account)
                .append("&").append("flashsms").toString());//加密方式
        _log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
                + sp_account + ",calling_number=" + calling_number
                + ",called_number=" + called_number +  ",signal_type=" + signal_type
                + ",send_time_from=" + sms_create_time_from + ",send_time_to="
                + sms_create_time_to + ",mt_status=" + mt_status+ ",province_code="
                + province_code + ",per_page_size="
                + per_page_size + ",sp_pwd="+sp_pwd+",nowpwd="+nowpwd+",receive_time_from="+receive_time_from+
                ",receive_time_to="+receive_time_to+",send_number="+send_number+",receive_number="+receive_number
        +",flash_send_number="+flash_send_number+",create_user="+create_user+",product_id="+product_id);
        /**if(!nowpwd.equals(sendPwd)){
            setAttr("result_code",
                    EnumAPIResultCode.UNAUTHORIZED_BUSINESS.getValue());
            setAttr("result_msg", "鉴权失败:密码错误");
            renderJson();
            return;
        }
        if (StringUtils.isBlank(sp_account)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "账号不能为空");//账号为空会躲避密码验证
            renderJson();
            return;
        }**/
        /** if (StringUtils.isBlank(called_number)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "被叫不能为空");//
            renderJson();
            return;
        }**/
        StringBuffer strBuf = new StringBuffer();
        if (StringUtils.isNotBlank(called_number)) {
            called_number = StringUtils.trim(called_number);
            strBuf.append(" AND (called_number:");
            strBuf.append(called_number);
            strBuf.append(") ");
        }
        //strBuf.append("called_number:"+called_number);//default
        if (StringUtils.isNotBlank(calling_number)) {
            calling_number = StringUtils.trim(calling_number);
            strBuf.append(" AND (calling_number:");
            strBuf.append(calling_number);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(send_number)) {
            send_number = StringUtils.trim(send_number);
            strBuf.append(" AND (send_number:");
            strBuf.append(send_number);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(receive_number)) {
            receive_number = StringUtils.trim(receive_number);
            strBuf.append(" AND (receive_number:");
            strBuf.append(receive_number);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(flash_send_number)) {
            flash_send_number = StringUtils.trim(flash_send_number);
            strBuf.append(" AND (flash_send_number:");
            strBuf.append(flash_send_number);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(create_user)) {
            create_user = StringUtils.trim(create_user);
            strBuf.append(" AND (create_user:");
            strBuf.append(create_user);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(product_id)) {
            product_id = StringUtils.trim(product_id);
            strBuf.append(" AND (product_id:");
            strBuf.append(product_id);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(signal_type)) {
            signal_type = StringUtils.trim(signal_type);
            strBuf.append(" AND (signal_type:");
            strBuf.append(signal_type);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(province_code)) {
            province_code = StringUtils.trim(province_code);
            strBuf.append(" AND (province_code:");
            strBuf.append(province_code);
            strBuf.append(") ");
        }
        if (StringUtils.isNotBlank(mt_status)) {
            mt_status = StringUtils.trim(mt_status);
            strBuf.append(" AND (mt_status:");
            strBuf.append(mt_status);
            strBuf.append(") ");
        }
        // 类型是string,只能精确到天；如果需要精确到秒，需要solr端修改成date类型
        if (StringUtils.isNotBlank(sms_create_time_from)) {
            sms_create_time_from = StringUtils.trim(sms_create_time_from).replace("-","").replace(" ","").replace(":","");
            strBuf.append(" AND (send_time:[");
            strBuf.append(sms_create_time_from);
            strBuf.append(" TO ");
            if (StringUtils.isNotBlank(sms_create_time_to)) {
                strBuf.append(StringUtils.trim(sms_create_time_to).replace("-","").replace(" ","").replace(":",""));
            } else {
                strBuf.append("*");
            }
            strBuf.append("]) ");
        } else if (StringUtils.isNotBlank(sms_create_time_to)) {
            sms_create_time_to = StringUtils.trim(sms_create_time_to).replace("-","").replace(" ","").replace(":","");
            strBuf.append(" AND (send_time:[* TO ");
            strBuf.append(StringUtils.trim(sms_create_time_to));
            strBuf.append("]) ");
        }
        //receive time
        if (StringUtils.isNotBlank(receive_time_from)) {
            receive_time_from = StringUtils.trim(receive_time_from).replace("-","").replace(" ","").replace(":","");
            strBuf.append(" AND (receive_time_str:[");
            strBuf.append(receive_time_from);
            strBuf.append(" TO ");
            if (StringUtils.isNotBlank(receive_time_to)) {
                strBuf.append(StringUtils.trim(receive_time_to).replace("-","").replace(" ","").replace(":",""));
            } else {
                strBuf.append("*");
            }
            strBuf.append("]) ");
        } else if (StringUtils.isNotBlank(receive_time_to)) {
            receive_time_to = StringUtils.trim(receive_time_to).replace("-","").replace(" ","").replace(":","");
            strBuf.append(" AND (receive_time_str:[* TO ");
            strBuf.append(StringUtils.trim(receive_time_to));
            strBuf.append("]) ");
        }
        String querySql = strBuf.toString();
        //去除开头的AND
        if(StringUtils.isBlank(querySql)){
            querySql="*:*";
        }else{
            querySql=querySql.substring(4);
        }
        _log.info("solr查询语句:" + querySql);
        try {
            setAttr("streaming_no", streaming_no);
            SolrDocumentList docList = SolrCommonUtil.search(PropertieUtils
                            .getProperty("collectionSecurityFlashSms"),querySql,"id,mt_msg_id,invoke_code,invoke_msg,sms_create_time,calling_number,called_number,send_number,receive_number,flash_send_number,call_time,signal_type,\n" +
                            "province_code,signal_create_time,sms_content,mt_status,receive_time,mt_type,mt_err_code,mt_total,create_user","sms_create_time",SolrQuery.ORDER.desc,
                    Integer.valueOf(current_page),
                    Integer.valueOf(per_page_size));
            setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
            List list = SolrObject.toBeanList(docList,
                    SecurityFlashSmsVO.class);
            setAttr("result_msg", list);// 返回详情数据
            setAttr("result_num", docList.getNumFound());
        } catch (Exception e) {
            _log.info("solr查询异常:" + e);
            setAttr("result_code", EnumAPIResultCode.SYSTEM_ERROR.getValue());
            setAttr("result_msg", EnumAPIResultCode.SYSTEM_ERROR.getName());
        }
        renderJson();
        return;
    }
}